package com.cdm.forum.service.impl;

import com.cdm.forum.common.Result;
import com.cdm.forum.common.ResultCode;
import com.cdm.forum.dao.BoardMapper;
import com.cdm.forum.exception.ApplicationException;
import com.cdm.forum.model.Board;
import com.cdm.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Autowired
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        if(num <= 0) {
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE.toString()));

        }
        //调用dao
        List<Board> result = boardMapper.selectByNum(num);
        //校验
        if(result == null || result.isEmpty()) {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE.toString()));
        }
        return result;
    }

    /**
     * 更新板块发帖数 +1 操作
     * @param id
     */
    @Override
    public void addOneArticleCountById(Long id) {
        if(id == null|| id <= 0 ) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE.toString()));
        }

        //查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null) {
            //打印日志
            log.warn("{}board 的id:{}", ResultCode.ERROR_IS_NULL.toString(), id);
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.ERROR_IS_NULL.toString()));
        }

        //更新帖子数量
        Board updateBoard = new Board(); // 重新创建一个类是更新ArticleCount的值，如果在原来类上改会更新全部的值
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        int num = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //校验
        if(num != 1) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
    }

    @Override
    public Board selectByPrimaryKey(Long id) {
        if(id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString() + "id值<=0, id:" + id );
            throw new ApplicationException(Result.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 查找用户信息
        Board board = boardMapper.selectByPrimaryKey(id);

        return board;
    }


    @Override
    public void subOneArticleCountById(Long id) {
        if(id == null || id <= 0 ) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE.toString()));
        }

        //根据对应id查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null) {
            //打印日志
            log.warn("{}board 的id:{}", ResultCode.ERROR_IS_NULL.toString(), id);
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.ERROR_IS_NULL.toString()));
        }

        //更新帖子数量
        Board updateBoard = new Board(); // 重新创建一个类是更新ArticleCount的值，如果在原来类上改会更新全部的值
        updateBoard.setId(board.getId());
        if(board.getArticleCount() - 1 <= 0) {
            updateBoard.setArticleCount(0);
        }else {
            updateBoard.setArticleCount(board.getArticleCount() - 1);
        }

        //调用mapper
        int num = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //校验
        if(num != 1) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
    }

}
